<?php

/**
 * employee actions.
 *
 * @package    virtualstore
 * @subpackage employee
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class employeeActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        // sorting
        if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort'))) {
            $this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
        }


//        $this->employees = Doctrine::getTable('Employee')
//                        ->createQuery('a')
//                        ->execute();
        $items_x_page = sfConfig::get('app_maxEmployeePerPage');
        $this->pager = new sfDoctrinePager('Employee', $items_x_page); // Table, items per page
        //$this->pager->getQuery()->from('Employee e')->orderBy('e.lastname, e.lastname2, e.firstname DESC');
        $this->pager->setQuery($this->buildQuery());
        //$this->pager->getQuery(); // items query
        $this->pager->setPage($request->getParameter('page', 1)); // actual page
        $this->pager->init();
        $this->sort = $this->getSort();
    }

    public function executeShow(sfWebRequest $request) {
        $this->employee = Doctrine::getTable('Employee')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->employee);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new EmployeeForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new EmployeeForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($employee = Doctrine::getTable('Employee')->find(array($request->getParameter('id'))), sprintf('Object employee does not exist (%s).', $request->getParameter('id')));
        $this->form = new EmployeeForm($employee);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($employee = Doctrine::getTable('Employee')->find(array($request->getParameter('id'))), sprintf('Object employee does not exist (%s).', $request->getParameter('id')));
        $this->form = new EmployeeForm($employee);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($employee = Doctrine::getTable('Employee')->find(array($request->getParameter('id'))), sprintf('Object employee does not exist (%s).', $request->getParameter('id')));
        $employee->delete();

        $this->redirect('employee/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $employee = $form->save();

            $this->redirect('employee/edit?id=' . $employee->getId());
        }
    }

    protected function buildQuery() {
        $et = Doctrine_Core::getTable('Employee');

        $query = $et->createQuery();

        $this->addSortQuery($query);

        return $query;
    }

    protected function addSortQuery($query) {
        if (array(null, null) == ($sort = $this->getSort())) {
            return;
        }

        if (!in_array(strtolower($sort[1]), array('asc', 'desc'))) {
            $sort[1] = 'asc';
        }
        $query->addOrderBy($sort[0] . ' ' . $sort[1]);

    }

    protected function getSort() {
        if (null !== $sort = $this->getUser()->getAttribute('employee.sort', null, 'admin_module')) {
            return $sort;
        }

        $this->setSort(array(null, null));

        return $this->getUser()->getAttribute('employee.sort', null, 'admin_module');
    }

    protected function setSort(array $sort) {
        if (null !== $sort[0] && null === $sort[1]) {
            $sort[1] = 'asc';
        }
        $this->logMessage('Setting Sort ' . $sort[0] . " : " . $sort[1], 'debug');
        $this->getUser()->setAttribute('employee.sort', $sort, 'admin_module');
    }

    protected function isValidSortColumn($column) {
        return Doctrine::getTable('Employee')->hasColumn($column);
    }

}
